{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 11 for语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【选择】下列说法中，对Python中的for语句描述错误的是：（ ）\n",
    "\n",
    "A.Python中for语句只有一种写法：“for...in...”  \n",
    "B.for语句可以用break终止当前循环，重新进入循环  \n",
    "C.continue语句可以跳过循环的当前一步  \n",
    "D.for语句可以有else部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【填空】请写出下列代码运行的结果：（ ）  \n",
    "`\n",
    "sum = 0\n",
    "for i in range(1, 11):\n",
    "     if i % 2 == 0:\n",
    "        continue\n",
    "     if i % 10 == 5:\n",
    "        break\n",
    "     sum = sum + i\n",
    "print(sum)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【填空】写出下列Python语句的运行结果：（ ）  \n",
    "`\n",
    "for i in range(2,5):\n",
    "    print(i,end=\",\")`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：2,3,4,"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【判断】规定变量a = list(range(3))，则a的值为[1,2,3]。（ ）\n",
    "\n",
    "A.√  \n",
    "B.×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.【选择】下列语句的执行结果为：（ ）  \n",
    "`\n",
    "for i in range(4):\n",
    "    if i%2:\n",
    "        print(\"*\",end=\" \")\n",
    "    else:\n",
    "        continue\n",
    "    print(\"#\",end=\" \")\n",
    "else:\n",
    "    print(\"&\")`\n",
    "\n",
    "A.# * # * &  \n",
    "B.* # * # &  \n",
    "C.# * # *   \n",
    "D.* # * #"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.【编程】请编程计算1+3...+99+101。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "sum = 0\n",
    "for i in range(1,102,2):\n",
    "    sum += i\n",
    "print(sum)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.【编程】统计字符串“hfuiwofsdiobais”中字母“i”出现的次数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "s = \"hfuiwofsdiobais\"\n",
    "count = 0\n",
    "for i in s:\n",
    "    if (i == \"i\"):\n",
    "        count += 1\n",
    "print(count)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8.【编程】输入正整数n，判断其是否为素数。  \n",
    "注：素数指仅能被1和它本身整除的正整数（1除外）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "a = int(input(\"请输入一个正整数：\"))\n",
    "for i in range(2,a):\n",
    "    if a%i == 0:\n",
    "        print(a,\"不是素数\")\n",
    "        break\n",
    "if i == a-1:\n",
    "    print(a,\"是素数\")`\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
